package com.xiaoshuidi.cloud.module.rooms.job.delayjob.strategy;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomel.vo.RoomelUpdateReqVO;
import com.xiaoshuidi.cloud.module.rooms.enums.RoomHouseReservePaymentStatus;
import com.xiaoshuidi.cloud.module.rooms.enums.RoomHouseReserveStatus;
import com.xiaoshuidi.cloud.module.rooms.mq.producer.RoomEsProducer;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomHouse;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomHouseReserve;
import com.xiaoshuidi.cloud.module.rooms.repository.RoomHouseRepository;
import com.xiaoshuidi.cloud.module.rooms.repository.RoomHouseReserveRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static com.xiaoshuidi.cloud.module.rooms.enums.ErrorCodeConstants.RESERVE_INFO_NOT_EXISTS;

/**
 * 定金支付期限到期处理
 */
@Slf4j
@Component
public class DepositPaymentTermStrategy implements IStrategy {

    @Resource
    private RoomHouseReserveRepository reserveRepository;
    @Resource
    private RoomHouseRepository roomHouseRepository;
    @Resource
    private RoomEsProducer roomEsProducer;

    private static String toBePayCancelReason = "未支付到期取消";
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void consumerTask(Object taskParam) {
        log.info("定金支付期限到期处理:{}", JsonUtils.toJsonString(taskParam));
        Long id = (Long) taskParam;
        RoomHouseReserve roomHouseReserve = reserveRepository.getById(id);
        if(ObjectUtil.isEmpty(roomHouseReserve)){
            throw new ServiceException(RESERVE_INFO_NOT_EXISTS);
        }
        if(!roomHouseReserve.getStatus().equals(RoomHouseReserveStatus.TO_BE_PAID.getCode())){
            return;
        }
        String expireTime = LocalDateTimeUtil.format(roomHouseReserve.getDepositPaymentTerm(), "yyyy-MM-dd HH:mm");
        String excuteTime = LocalDateTimeUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm");
        log.info("到期时间:{}, 当前时间:{}", expireTime, excuteTime);
        if(!expireTime.equals(excuteTime)){
            return;
        }
        roomHouseReserve.setStatus(RoomHouseReserveStatus.CANCELED.getCode());
        roomHouseReserve.setCancelTime(LocalDateTime.now());
        roomHouseReserve.setCancelReason(toBePayCancelReason);
        roomHouseReserve.setPaymentStatus(RoomHouseReservePaymentStatus.CANCELED.getCode());
        reserveRepository.updateById(roomHouseReserve);

        RoomHouse roomHouse = new RoomHouse();
        roomHouse.setId(roomHouseReserve.getRoomHouseId());
        roomHouse.setIsLock(Boolean.FALSE);
        roomHouseRepository.updateById(roomHouse);

        List<RoomelUpdateReqVO> roomEsVoList = new ArrayList<>();
        RoomelUpdateReqVO roomelUpdateReqVO = new RoomelUpdateReqVO();
        roomelUpdateReqVO.setIsLock(Boolean.FALSE);
        roomelUpdateReqVO.setId(roomHouseReserve.getRoomHouseId());
        roomEsVoList.add(roomelUpdateReqVO);
        roomEsProducer.updateBatchRoomEl(roomEsVoList);
    }
}
